//
//  YaoHJHViewController.m
//  KuWoCan
//
//  Created by hwd on 17/3/3.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "YaoHJHViewController.h"
#import "YaoHKJHCell.h"
#import "NWEnquiryPlanRequest.h"
#import "PlanListModel.h"
#import "DetailJXKCViewController.h"
#import "EnquiryPlanCommitVC.h"

@interface YaoHJHViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableV;

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,strong) NSMutableArray *tableArray;

@end

@implementation YaoHJHViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTitle:@"我的要货计划"];
    [self.view addSubview:self.addTableView];
    [self createRightButtonWithImage:kImg(@"add") selector:@selector(addPlan)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setRefreshControl];
}


- (void)addPlan
{
    EnquiryPlanCommitVC *vc = [[EnquiryPlanCommitVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)setRefreshControl
{
    __weak typeof(self) weakSelf = self;
    _tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _currentPage = 1;
        [weakSelf loadingDataWithTag:1 showLoading:YES];
    }];
    [_tableV.mj_header beginRefreshing];
    
}

- (void)showLoadMoreControl:(BOOL)show
{
    if (show)
    {
        if (_tableV.mj_footer)
        {
            _tableV.mj_footer.hidden = NO;
        }else
        {
            __weak typeof(self) weakSelf = self;
            _tableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                // 进入加载状态后会自动调用这个block
                [weakSelf loadingDataWithTag:1 showLoading:YES];
            }];
        }
    }else
    {
        if (_tableV.mj_footer)
        {
            _tableV.mj_footer.hidden = YES;
        }
    }
    
}

- (void)loadingDataWithTag:(NSInteger)tag showLoading:(BOOL)isShowLoading
{
    __weak typeof(self) weakSelf = self;
    NWEnquiryPlanRequest *request = [[NWEnquiryPlanRequest alloc] init];
    [request startRequestGetPlanListWithUserId:[Singleton shareInstance].userId pageNo:[NSString stringWithFormat:@"%ld",(long)_currentPage] pageSize:kPageSize];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (tag == 1)
        {
            [_tableV.mj_header endRefreshing];
        }else
        {
            [_tableV.mj_footer endRefreshing];
        }
        if (succ) {
            if (_currentPage == 1)
            {
                if (_tableArray == nil)
                {
                    _tableArray = [[NSMutableArray alloc] init];
                }else{
                    [_tableArray removeAllObjects];
                }
            }
            if (result && result != nil)
            {
                NSArray *arr = [PlanListModel arrayOfModelsFromDictionaries:result];
                
                if ([arr count] == [kPageSize integerValue])
                {
                    _currentPage ++ ;//有下一页  show 加载按钮
                    [weakSelf showLoadMoreControl:YES];
                    [_tableArray addObjectsFromArray:arr];
                    [_tableV reloadData];
                }else if ([arr count] == 0)
                {
                    //没有下一页  hide 加载按钮
                    [weakSelf showLoadMoreControl:NO];
                    [_tableV reloadData];
                    
                }else
                {
                    //没有下一页  hide 加载按钮
                    [weakSelf showLoadMoreControl:NO];
                    [_tableArray addObjectsFromArray:arr];
                    [_tableV reloadData];
                }
                
                
            }
        }
    }];
}



-(UITableView *)addTableView//添加TableView
{
    if (!_tableV) {
        _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0,0,SCREENW,SCREENH) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = [UIColor colorWithHexString:@"F6F7F8"];
        _tableV.dataSource = self;
        _tableV.delegate = self;
        _tableV.separatorStyle = UITableViewCellSelectionStyleNone;
        [self.view addSubview:_tableV];
    }
    return _tableV;
    
}
#pragma mark - TableView专区
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75*screenW_6;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15*screenW_6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YaoHKJHCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell%d",(int)indexPath.row]];
    if (cell == nil) {
        cell  = [[YaoHKJHCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%d",(int)indexPath.row]];
    }
    [cell loadCellWith:nil];
    cell.model = _tableArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailJXKCViewController *planV = [[DetailJXKCViewController alloc]init];
    PlanListModel *model = _tableArray[indexPath.row];
    planV.cargoPlanId = model.id;
    planV.businessType = BusinessTypeEnquirPlan;
    [self.navigationController pushViewController:planV animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NWEnquiryPlanRequest *request = [[NWEnquiryPlanRequest alloc] init];
    PlanListModel *model = _tableArray[indexPath.row];
    [request startRequestDeletePlanWithId:model.id];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ) {
            [self viewWillAppear:YES];
            [SVProgressHUD showSuccessWithStatus:@"删除成功"];
        }
    }];
}


@end
